package cn.abyss.admin.ums.service.impl;

import cn.abyss.admin.consts.AdminWebConstants;
import cn.abyss.admin.enums.AdminWebExceptionEnum;
import cn.abyss.admin.enums.BaseAdminWebExceptionLevelEnum;
import cn.abyss.admin.exception.AdminWebException;
import cn.abyss.admin.ums.dao.UmsAdminLoginLogMapper;
import cn.abyss.admin.ums.dao.UmsAdminMapper;
import cn.abyss.admin.ums.model.UmsAdmin;
import cn.abyss.admin.ums.model.UmsAdminLoginLog;
import cn.abyss.admin.ums.service.UmsAdminService;
import cn.abyss.admin.util.AbyssAdminDataValidator;
import cn.abyss.admin.util.ResultMap;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:
 *
 * @author zhangsj
 * @date Created on 2021/8/19
 */
@Service
public class UmsAdminServiceImpl implements UmsAdminService {
    @Autowired
    private UmsAdminMapper umsAdminMapper;
    @Autowired
    private UmsAdminLoginLogMapper umsAdminLoginLogMapper;

    @Override
    public void insert(UmsAdmin umsAdmin) throws Exception {
        int row = umsAdminMapper.insertUseGeneratedKeys(umsAdmin);
        if (row != 1) {
            throw new AdminWebException(AdminWebConstants.UMS_ADMIN,
                    AdminWebExceptionEnum.ADMIN_WEB_MEMBER_INSERT_FAIL, BaseAdminWebExceptionLevelEnum.INTERNAL_EXCEPTION);
        }
    }

    @Override
    public void update(UmsAdmin umsAdmin) throws Exception {
        int row = umsAdminMapper.updateByPrimaryKeySelective(umsAdmin);
        if (row != 1) {
            throw new AdminWebException(AdminWebConstants.UMS_ADMIN,
                    AdminWebExceptionEnum.ADMIN_WEB_MEMBER_UPDATE_FAIL, BaseAdminWebExceptionLevelEnum.INTERNAL_EXCEPTION);
        }
    }

    @Override
    public void delete(Long id) throws Exception {
        int row = umsAdminMapper.deleteByPrimaryKey(id);
        if (row != 1) {
            throw new AdminWebException(AdminWebConstants.UMS_ADMIN,
                    AdminWebExceptionEnum.ADMIN_WEB_MEMBER_DELETE_FAIL, BaseAdminWebExceptionLevelEnum.INTERNAL_EXCEPTION);
        }
    }

    @Override
    public UmsAdmin getById(Long id) throws Exception {
        return umsAdminMapper.selectByPrimaryKey(id);
    }

    @Override
    public Map<String, Object> listForPage(Map<String, Object> map, int pageNum, int pageSize) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        PageHelper.startPage(pageNum, pageSize, true);
        List<ResultMap> list = umsAdminMapper.queryUmsAdminMap(map);
        PageInfo<ResultMap> page = new PageInfo<>(list);
        resultMap.put("pageSize", page.getPageSize());
        resultMap.put("pageNum", page.getPageNum());
        resultMap.put("total", page.getTotal());
        resultMap.put("list", list);
        return resultMap;
    }

    @Override
    public List<UmsAdmin> query(Map<String, Object> map) throws Exception {
        Example example = new Example(UmsAdmin.class);
        Criteria criteria = example.createCriteria();
        if (!AbyssAdminDataValidator.isEmptyId((Long) map.get("id"))) {
            criteria.andEqualTo("id", map.get("id"));
        }
        if (!AbyssAdminDataValidator.isEmptyId((Long) map.get("status"))) {
            criteria.andEqualTo("status", map.get("status"));
        }
        if (!StringUtils.isEmpty(map.get("username"))) {
            criteria.andEqualTo("username", map.get("username"));
        }
        return umsAdminMapper.selectByExample(example);
    }

    @Override
    public ResultMap getSummation(Map<String, Object> map)  throws Exception {
        List<ResultMap> list = umsAdminMapper.sumUmsAdmin(map);
        if(list == null || list.size() != 1){
            throw new AdminWebException(AdminWebConstants.UMS_ADMIN,
                    AdminWebExceptionEnum.ADMIN_WEB_MEMBER_SUM_QUERY_FAIL, BaseAdminWebExceptionLevelEnum.INTERNAL_EXCEPTION);
        }
        return list.get(0);
    }

    @Override
    public void insertLoginLog(UmsAdminLoginLog loginLog) {
        int row = umsAdminLoginLogMapper.insertUseGeneratedKeys(loginLog);
        if (row != 1) {
            throw new AdminWebException(AdminWebConstants.UMS_ADMIN,
                    AdminWebExceptionEnum.ADMIN_WEB_LOGIN_LOG_INSERT_FAIL, BaseAdminWebExceptionLevelEnum.INTERNAL_EXCEPTION);
        }

    }
}
